package com.whity.towerdefense.model;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;
import android.util.Log;

import com.whity.towerdefense.view.GraphicDrawable;
import com.whity.towerdefense.view.GraphicElt;
import com.whity.towerdefense.view.GraphicEltFactory;
import com.whity.towerdefense.view.GraphicHeart;

public class Tower extends GraphicElt implements I_Tickable
{
	// =============================================================
	// Private attributs :
	
	private final static String			TAG = "com.whity.towerdefense.model";
	
	// General attributs
	@SuppressWarnings("unused")
	private final String 				m_TypeName;
	
	@SuppressWarnings("unused")
	private final float					m_OriLife;
	@SuppressWarnings("unused")
	private final float					m_OriArmor;
	private final ArrayList<ShootType> 	m_ShootTypes;
	private ArrayList<Integer> 			m_ShootsReloadCounters;
	private ArrayList<ShootType> 		m_ShootsReady;
	
	private GraphicElt					m_Range;
	
	@SuppressWarnings("unused")
	private float						m_OriHpRegen;
	
	// =============================================================
	// Protected functions :
	
	public Tower(String p_TypeName, float p_Life, float p_Armor, float p_OriHpRegen, float p_Size, GraphicHeart p_GraphicHeart, ArrayList<ShootType> p_ShootTypes, float p_X, float p_Y)
	{
		super(p_GraphicHeart);
		m_TypeName		= p_TypeName;
		m_OriLife		= p_Life;
		m_OriArmor		= p_Armor;
		m_OriHpRegen	= p_OriHpRegen;
		m_ShootTypes	= p_ShootTypes;
		
		m_ShootsReady	= new ArrayList<ShootType>();
		m_ShootsReloadCounters = new ArrayList<Integer>();
		for(ShootType shootType : m_ShootTypes)
		{
			m_ShootsReady.add(shootType);
			m_ShootsReloadCounters.add(0);
		}
		
		m_Position 			= new PointF(p_X, p_Y);
		SetScale(p_Size/(GetWidth()/Map.UNIT_SIZE));
		SetPosition(m_Position.x * Map.UNIT_SIZE - GetWidth()*GetScaleX()/2, m_Position.y * Map.UNIT_SIZE - GetHeight()*GetScaleY()/2);
		
		
		m_Range = GraphicEltFactory.CreateGraphicElt(m_ShootTypes.get(0).GetRangeGraphicHeartPath());
		
		m_Range.SetScale((m_ShootTypes.get(0).GetRange()/(m_Range.GetWidth()/Map.UNIT_SIZE))/GetScale());
		m_Range.SetPosition((p_Size/(2.0f * GetScale())) * Map.UNIT_SIZE - m_Range.GetWidth()*m_Range.GetScaleX()/2, (p_Size/(2.0f * GetScale())) *  Map.UNIT_SIZE - m_Range.GetHeight()*m_Range.GetScaleY()/2);
		
		m_Range.SetAlpha(0.5f);
		
	}
	
	// =============================================================
	// Public functions

	public boolean GameTick(int p_Ms)
	{
		for(int i = 0 ; i < m_ShootsReady.size() ; i++)
		{
			ShootType shootType = m_ShootsReady.get(i);
			if(shootType.Shoot(m_Position.x/Map.UNIT_SIZE+0.5f, m_Position.y/Map.UNIT_SIZE+0.5f))
			{
				m_ShootsReady.remove(i);
				i--;
			}
		}
		
		for(int i = 0 ; i < m_ShootTypes.size() ; i++)
		{
			ShootType shootType = m_ShootTypes.get(i);
			if(!m_ShootsReady.contains(shootType))
			{
				Integer counter = m_ShootsReloadCounters.get(i);
				counter += p_Ms;
				if(counter > (1000.0f / shootType.GetFireRate()))
				{
					m_ShootsReady.add(shootType);
					counter = 0;
				}
				m_ShootsReloadCounters.set(i, counter);
			}
		}
		return true;
	}
	
	public void DisplayRange(boolean p_State)
	{
		if(p_State)
		{
			AddSon(m_Range);
		}
		else
		{
			RemoveSon(m_Range);
		}
	}
	
	@ Override
	public void SetPosition(float p_X, float p_Y)
	{
		m_Position.x = p_X * Map.UNIT_SIZE - GetWidth()*GetScaleX()/2;
		m_Position.y = p_Y * Map.UNIT_SIZE - GetHeight()*GetScaleY()/2;
	}
	
	// =============================================================
}
